package cn.lili.controller.operationcenter;

import cn.lili.cache.limit.annotation.LimitPoint;
import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.client.UserClient;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.operationcenter.entity.dos.OperationCenter;
import cn.lili.modules.operationcenter.entity.vos.OperationCenterSearchParams;
import cn.lili.modules.operationcenter.entity.vos.OperationCenterVo;
import cn.lili.modules.service.OperationCenterService;
import cn.lili.modules.system.entity.dos.Region;
import cn.lili.modules.urbansubstation.entity.dos.UrbanSubstation;
import cn.lili.modules.urbansubstation.entity.vos.UrbanSubstationGetVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@Api(tags = "运营中心控制器")
@RequestMapping("/operationCenter")
@RequiredArgsConstructor
public class OperationCenterController {

    private final OperationCenterService operationCenterService;

    private final UserClient userClient;

    @GetMapping("/getOperationCenterInfo")
    @ApiOperation("获取运营中心信息")
    public ResultMessage<OperationCenter> getOperationCenterInfo() {
        AuthUser authUser = UserContext.getCurrentUser();
        return ResultUtil.data(operationCenterService.getOperationCenter(authUser.getId()));
    }

    /**
     * 发送短信验证码
     * @param
     * @return
     */
    @PostMapping("/sendSMSVerCode")
    @ApiOperation("运营中心发送验证码")
    @PreventDuplicateSubmissions
    @LimitPoint(name = "send_smscode_opertion", key = "verificationOperation")
    public ResultMessage<Object> sendSMSVerificationCode(
            @RequestHeader String uuid
    ) {
        return operationCenterService.sendSMSVerificationCode(uuid);
    }



    @PostMapping("/setBankAccountByOperationId")
    @ApiOperation("设置运营中心提现银行账户")
    @PreventDuplicateSubmissions
    public ResultMessage<OperationCenter> setBankAccountByOperationId(@RequestBody OperationCenter operationCenter) {
        return operationCenterService.setBankAccountByOperationId(operationCenter,"");
    }

    @GetMapping
    @ApiOperation("分页获取运营中心列表")
    public ResultMessage<Page<OperationCenterVo>> getByPage(OperationCenterSearchParams operationCenterSearchParams, PageVO page) {
        return ResultUtil.data(operationCenterService.findByConditionPage(operationCenterSearchParams, page));
    }

    @PostMapping("/add")
    @ApiOperation("添加运营中心")
    public ResultMessage<Object> add (@RequestBody OperationCenter operationCenter) {
        operationCenterService.add(operationCenter);
        return ResultUtil.success();
    }

    @PostMapping("/edit")
    @ApiOperation("编辑运营中心")
    public ResultMessage<Object> edit (@RequestBody OperationCenter operationCenter) {
        operationCenterService.edit(operationCenter);
        return ResultUtil.success();
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除运营中心")
    public ResultMessage<Object> delete (@PathVariable String id) {
        operationCenterService.delete(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "关闭运营中心")
    @ApiImplicitParam(name = "id", value = "运营中心id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/disable/{id}")
    public ResultMessage<Object> disable(@PathVariable String id) {
        operationCenterService.disable(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "开启运营中心")
    @ApiImplicitParam(name = "id", value = "运营中心id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/enable/{id}")
    public ResultMessage<Object> enable(@PathVariable String id) {
        operationCenterService.enable(id, null);
        return ResultUtil.success();
    }

    @ApiOperation(value = "审核运营中心")
    @ApiImplicitParam(name = "id", value = "运营中心id", required = true, dataType = "String", paramType = "path")
    @PutMapping(value = "/updateStatus/{id}/{status}")
    public ResultMessage<Object> updateStatus(@PathVariable String id, @PathVariable String status) {
        operationCenterService.enable(id, status);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取自己运营中心信息")
    @GetMapping(value = "/owner")
    public ResultMessage<OperationCenter> get() {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        OperationCenter operationCenter = operationCenterService.getOperationCenter(currentUser.getId());
        User user = userClient.getById(currentUser.getId());
        if (user!=null) {
            operationCenter.setOpenId(user.getOpenId());
            operationCenter.setAccount(user.getAccount());
            operationCenter.setFyName(user.getFyName());
        }
        //获取当前登录商家内容
        return ResultUtil.data(operationCenter);
    }

    @ApiOperation(value = "获取运营中心下面的市")
    @GetMapping(value = "/getCityList")
    public ResultMessage<List<Region>> getRegionByParentId() {
        return ResultUtil.data(operationCenterService.getRegionByParentId());
    }

}
